package wili.logic;

import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import wili.c.Ca;
import wili.c.File9a;

/**
 * @author weila 2022年10月4日
 */
public class StrToListMapFromFile {
	public final String name;

	final File file;

	final Map<String, Long> modifiedMap = new TreeMap<>();

	final Map<String, Map<String, List<Object>>> maps = new HashMap<>();

	public String toString() {
		return file.getName() + "," + maps.size() + "" + maps.keySet();
	}

	public StrToListMapFromFile(File file) {
		this.name = file.getName();
		this.file = file;
		Ca.asert(file.exists(), "FileNotFound: " + file);
	}

	@SuppressWarnings("all")
	public Map<String, List<Object>> getMap() {
		Long time = modifiedMap.get(name);
		if (time != null && file.lastModified() <= time)
			return maps.get(name);
		Map<String, String> map = File9a.mapOfFile(file, "=");
		Map<String, List<Object>> map2 = new TreeMap<>();
		for (String key : map.keySet()) {
			String st = map.get(key);
			List<Object> lis = Arrays.asList(st.split("\\s*,\\s*"));
			for (int i = 0; i < lis.size(); i++) {
				String ele = lis.get(i).toString();
				if (ele.startsWith("'")) {
				} else {
					try {
						Integer num = Integer.parseInt(ele);
						lis.set(i, num);
					} catch (Exception e) {
					}
				}
			}
			map2.put(key, lis);
		}
		maps.put(name, map2);
		modifiedMap.put(name, file.lastModified());
		return map2;
	}
}
